package com.flute.icrawler.policy.updatepolicy.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class HtmlParser {

	/**
	 * 过滤掉html中不需要的节点
	 * 
	 * @param content
	 * @return
	 */
	public String getHtml(String content) {
		Document doc = this.nekoParser(new ByteArrayInputStream(content.getBytes()));
		StringBuffer buffer = new StringBuffer("");
		this.transform(doc, buffer);
		return buffer.toString();
	}

	/**
	 * 
	 * @param in
	 * @return
	 */
	private Document nekoParser(InputStream in) {
		try {
			DOMParser parser = new DOMParser();
			parser.setFeature("http://xml.org/sax/features/namespaces", false);
			parser.setProperty("http://cyberneko.org/html/properties/names/elems", "upper");
			parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");
			parser.parse(new InputSource(new InputStreamReader(in)));
			return parser.getDocument();
		} catch (SAXException e) {
			throw new RuntimeException(e.getMessage());
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}

	/**
	 * 
	 * @param node
	 * @param buffer
	 */
	private void transform(Node node, StringBuffer buffer) {
		if (node.getNodeType() == Node.TEXT_NODE) {
			if (node.getNodeValue() != null && !node.getNodeValue().equals("")) {
				buffer.append(node.getNodeValue());
			}
		} else if (node.getNodeType() == Node.ELEMENT_NODE) {
			String tag = node.getNodeName().toUpperCase();
			// 过滤不需要的节点
			if (tag.equals("HEAD") || tag.equals("META") || tag.equals("STYLE")
					|| tag.equals("SCRIPT") || tag.equals("LINK") || tag.equals("IFRAME")
					|| tag.equals("FREAME") || tag.equals("NOSCRIPT") || tag.equals("EMBED")) {
				return;
			}
			buffer.append("<").append(tag);
			NamedNodeMap attrs = node.getAttributes();
			for (int i = 0; i < attrs.getLength(); i++) {
				Node attr = attrs.item(i);
				buffer.append(" ").append(attr.getNodeName().toLowerCase()).append("='").append(
						attr.getNodeValue()).append("'");
			}
			buffer.append(">");
			if (node.hasChildNodes()) {
				NodeList nodes = node.getChildNodes();
				for (int j = 0; j < nodes.getLength(); j++) {
					this.transform(nodes.item(j), buffer);
				}
			}
			buffer.append("</").append(tag).append(">");
		} else if (node.getNodeType() == Node.DOCUMENT_NODE) {
			this.transform(((Document) node).getDocumentElement(), buffer);
		} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
			buffer.append(node.getNodeValue());
		} else if (node.getNodeType() == Node.COMMENT_NODE) {
			return;
		}
	}

}
